import urllib2

from BeautifulSoup import *

from PIL import Image,ImageDraw, ImageFont

import re
import DTW

def DrawLine(data, draw, color):


   # f = ImageFont.truetype("andalemo.ttf", 100)
    
    x = 10
    y = 50*100
    Normalize(data)
    for i in range(len(data)-1):
        # Draw the condition string
        #draw.text((x,y), data[0][-5:], (0,0,0), font=f)
        # Draw links to the branches
        draw.line((x-10, h*(1 - data[i]), x, h*(1 - data[i+1])), fill=color, width=2)
        x += 10


def Normalize(text):
    max_1 = 0
    for sample in text:
        if sample > max_1:
            max_1 = sample
    for sample_id in range(len(text)):
        text[sample_id] /= max_1





def GetData(stock, year, quarter):

    url = "http://money.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/" + str(stock)+".phtml?year=" + str(year) + "&jidu=" + str(quarter)
    p_date_url = re.compile("http://vip.stock.finance.sina.com.cn/quotes_service/view/vMS_tradehistory\Sphp\Ssymbol=(\S+)&\S+date=(\S+)\"")
    p_value = re.compile("\d+")
    f = open("test.txt", 'w')
    
    data = urllib2.urlopen(url) 
    soup = BeautifulSoup(data)
    bsoup = soup.prettify()
    lines = bsoup.split('\n')
    
    date_data = []
    
    for i in range(len(lines)):
        
        m_date_url = p_date_url.search(lines[i])
        
        if m_date_url:
            date = m_date_url.group(2)
            date_data.append([date])
            i += 4
            for j in range(6):
                for k in range(1,6):
                    #print i+k+5*j
                    m_value = p_value.search(lines[i+k+5*j])
                    #print lines[i+k+5*j]
                    if m_value:
                        date_data[-1].append(float(lines[i+k+5*j]))
            print date_data[-1]
    return date_data

date_data3 = GetData(600031, 2010, 3);
date_data4 = GetData(600031, 2010, 4); 
date_data1 = GetData(600031, 2011, 1);
date_data2 = GetData(600031, 2011, 2);
#date_data5 = GetData(600031, 2011, 3);
#date_data6 = GetData(600031, 2011, 4); 
#date_data7 = GetData(600031, 2011, 5);
#date_data8 = GetData(600031, 2011, 6);


date_data = date_data1 + date_data2 + date_data3 + date_data4
dump(date_data)
date_data.reverse()

w = 2000
h = 1000
img = Image.new('RGB',(w,h),(255,255,255))
draw = ImageDraw.Draw(img)


text = []
for data in date_data:
    text.append(data[3])
Normalize(text)

DrawLine(text, draw, (0,0,255))

pattern = []
for data in date_data3:
    pattern.append(data[3])
Normalize(pattern)

DrawLine(pattern, draw, (255,0,0))

img.save("stock.jpg",'JPEG')




# <html>
#  <head>
#   <title>
#    Page title
#   </title>
#  </head>
#  <body>
#   <p id="firstpara" align="center">
#    This is paragraph
#    <b>
#     one
#    </b>
#    .
#   </p>
#   <p id="secondpara" align="blah">
#    This is paragraph
#    <b>
#     two
#    </b>
#    .
#   </p>
#  </body>
# </html>